/**
 * Created by HuyNguyen on 3/19/14.
 */

var productCategoryWeb = function() {
    var $productLayout = $(".product-layout");

    // init all action in page
    var setupProduct = function(){
        $productLayout.find('.product').each(function() {
            var $this = $(this), added_classes = '';

            $this = ( ! $this.parent().hasClass('mix-item') ) ? $this.wrap('<div class="mix-item ' + added_classes + '" />').parent() : $this.parent();
            $this.children().css('visibility', 'visible');

            var $lazyLoad = $this.find('.lazyLoad');
            if ( $lazyLoad.length )
            {
                $this.addClass('loading');
            }

        });
    }
    // change style of list show product
    var initChangeLayout = function(){
        $('[data-layout="list"], [data-layout="grid"]').bind('click', function(e) {
            e.preventDefault();
            if($(this).hasClass("active")) return;
            $productLayout.hide().toggleClass('product-grid').toggleClass('product-list').fadeIn("slow");
            $(this).closest('div.display').find('.active').removeClass('active');
            $(this).addClass('active');
        });
    }

    // change style of list show product
    var initChangeSortOrder = function(){
        $('select[name="SORT_ORDER"]').bind('change', function(e) {
            $.ajax({
                url : $(location).attr('pathname'),
                data : {SORT_ORDER : $(this).val()},
                dataType :"html",
                success : function(html){
                    $productLayout.hide().empty().html(html).fadeIn("slow");
                }
            })
        });
    }
    return {
        init: function () {
           // setupProduct();


            initChangeLayout();
            initChangeSortOrder();

        }
    };
}();

/* Initialize WebApp when page loads */
$(function(){ productCategoryWeb.init(); });